package com.goldze.mvvmhabit.ui.chat;

import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.goldze.mvvmhabit.entity.response.GetHistoryMsgResponseEntity;
import com.goldze.mvvmhabit.utils.RetrofitClient;
import com.goldze.mvvmhabit.utils.UserManager;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class ChatContentListItemViewModel extends ItemViewModel<ChatContentListViewModel> {

    public ObservableField<GetHistoryMsgResponseEntity> entity = new ObservableField<>();

    public ChatContentListItemViewModel(@NonNull ChatContentListViewModel viewModel, GetHistoryMsgResponseEntity entity) {
        super(viewModel);
        this.entity.set(entity);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
        }
    });


    public String getMyName() {
        if (UserManager.getAppManager().getmUserInfo() != null) {
            return UserManager.getAppManager().getmUserInfo().getNickName();
        } else {
            return "";
        }
    }

    public String getFriendName() {
        if (UserManager.getAppManager().getGetChatUsers() != null) {
            return UserManager.getAppManager().getGetChatUsers().getFriendName();
        } else {
            return "";
        }
    }

    public Boolean isPic() {
        String content = entity.get().getContent();
        if (content != null && !content.isEmpty()) {
            if (content.startsWith("/") && content.endsWith(".jpeg")) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public Boolean isMy() {
        if (entity.get().getSenderId().equals(UserManager.getAppManager().getGetChatUsers().getFriendId())) {
            return false;
        } else {
            return true;
        }
    }

    public String getContentImageUrl() {
        return RetrofitClient.baseUrl + entity.get().getContent();
    }


    public String getMyIcon() {
        if (UserManager.getAppManager().getmUserInfo() != null) {
            return UserManager.getAppManager().getmUserInfo().getAvatar();
        } else {
            return "";
        }
    }

    public String getFriendIcon() {
        if (UserManager.getAppManager().getGetChatUsers() != null) {
            return UserManager.getAppManager().getGetChatUsers().getFriendAvatar();
        } else {
            return "";
        }
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }
}
